/*
 * Copyright (c) 2025 Huawei Device, Inc. Ltd. and <马弓手>.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "MediaPicker.h"
#include "FileCache.h"
#include "cJSON.h"
#include "../file/FileUtils.h"
#include <filemanagement/file_uri/oh_file_uri.h>

REGISTER_PLUGIN_CLASS(MediaPicker)
bool MediaPicker::execute(const string&  action, cJSON* args, CallbackContext cbc)
{
    if(action == "getMedias" || action == "photoLibrary") {
        m_cbc = cbc;
        cJSON* pJson = cJSON_GetArrayItem(args, 0);
        if(pJson != nullptr) {
            char* pStr = cJSON_Print(pJson);
            executeArkTs("/pages/MediaIndex", 0, pStr, "MediaPicker", cbc);
            free(pStr);
        }
    }
    
    if(action == "extractThumbnail") {
        m_cbc = cbc;
        cJSON* pJson = cJSON_GetArrayItem(args, 0);
        if(pJson != nullptr) {
            char* pStr = cJSON_Print(pJson);
            executeArkTs("/pages/MediaIndex", 3, pStr, "MediaPicker", cbc);
            free(pStr);
        }
    }
    if(action == "compressImage") {
        m_cbc = cbc;
        cJSON* pJson = cJSON_GetArrayItem(args, 0);
        if(pJson != nullptr) {
            char* pStr = cJSON_Print(pJson);
            executeArkTs("/pages/MediaIndex", 4, pStr, "MediaPicker", cbc);
            free(pStr);
        }
    }
    
    if(action == "getExifForKey") {
        m_cbc = cbc;
        char* pStr = cJSON_Print(args);
        executeArkTs("/pages/MediaIndex", 5, pStr, "MediaPicker", cbc);
        free(pStr);
    }
    
    if(action == "getFileInfo") {
        string strPath;
        cJSON* pPath = cJSON_GetArrayItem(args, 0);
        if(pPath != nullptr) {
            strPath = pPath->valuestring;
        }
        strPath = FileUtils::getNativeUrl(strPath);
        FileUtils::LocalFilesystemURL systemUrl = FileUtils::resolveNativeUri(strPath);
        cJSON* pRet = cJSON_CreateObject();
        cJSON_AddStringToObject(pRet, "path", systemUrl.m_strUri.c_str());
        cJSON_AddStringToObject(pRet, "uri", systemUrl.m_strUri.c_str());
        cJSON_AddStringToObject(pRet, "mediaType", "image");
        cJSON_AddNumberToObject(pRet, "size", systemUrl.m_lngSize);
        cbc.success(pRet);
        cJSON_Delete(pRet);
        return true;
    }   
    
    if(action == "onArKTsResult") {
        return onArKTsResult(args);
    }
    return true;
}

bool MediaPicker::onArKTsResult(cJSON* args)
{
    string content = cJSON_GetObjectItem(args, "content")->valuestring;
    cJSON* json = cJSON_GetObjectItem(args, "result");
    if(content == "getMedias") {
         if(json != NULL && json->type == cJSON_Array) {
            cJSON* pArray = cJSON_CreateArray();
            int nCount = cJSON_GetArraySize(json);
            for(int i=0; i<nCount; i++) {
                cJSON* pObject = cJSON_Parse(cJSON_GetArrayItem(json, i)->valuestring);
                if(pObject != nullptr) {
                    string strFileUri;
                    cJSON* pFileUri = cJSON_GetObjectItem(pObject, "uri");
                    if(pFileUri != nullptr && pFileUri->type == cJSON_String) {
                        strFileUri = pFileUri->valuestring;
                    }
                    
                    char *pathResult = NULL;
                    FileManagement_ErrCode ret = OH_FileUri_GetPathFromUri(strFileUri.c_str(), strFileUri.length(), &pathResult);
                    if (ret == 0 && pathResult != NULL) {
                        if(!FileCache::IsFile(pathResult, "")) {
                            m_cbc.error("failed");
                                return true;
                        }
                        cJSON* pObjPath = cJSON_CreateString(pathResult);
                        cJSON_AddItemToObject(pObject, "path", pObjPath);
                        free(pathResult);
                    }
                    cJSON_AddItemToArray(pArray, pObject);
               }
                
            }
            m_cbc.success(pArray);
            cJSON_Delete(pArray);
        }
    }
    
    if(content == "extractThumbnail") {
        int nCount = cJSON_GetArraySize(json);
        if(nCount > 0) {
            char* pStr = cJSON_GetArrayItem(json, 0)->valuestring;
            cJSON* pRet = cJSON_CreateObject();
            cJSON_AddStringToObject(pRet, "thumbnailBase64", pStr);
            m_cbc.success(pRet);
            cJSON_Delete(pRet);
        }
    }
    if(content == "compressImage") {
        int nCount = cJSON_GetArraySize(json);
        if(nCount > 0) {
            cJSON* pObj = cJSON_Parse(cJSON_GetArrayItem(json, 0)->valuestring);
            cJSON* pPath = cJSON_GetObjectItem(pObj, "path");
            if(pPath != nullptr) {
                string strPath = pPath->valuestring;
                char *uriResult = NULL;
                FileManagement_ErrCode ret = OH_FileUri_GetUriFromPath(strPath.c_str(), strPath.length(), &uriResult);
                if (ret == 0 && uriResult != NULL) {
                    cJSON_AddStringToObject(pObj, "uri", uriResult);
                    free(uriResult);
                }
            }
            m_cbc.success(pObj);
            cJSON_Delete(pObj);
        }
    }
    if(content == "getExifForKey") {
        int nCount = cJSON_GetArraySize(json);
        if(nCount > 0) {
            char* pStr = cJSON_GetArrayItem(json, 0)->valuestring;
            m_cbc.success(pStr);
        }
    }
    return true;
}